package com.fds.system.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class LoginUser implements UserDetails {
    private Long id;
    private String username;
    private String password;
    private String type;
    private Set<GrantedAuthority> authorities = new HashSet<>();
    // 序列化问题，上面的权限无法序列化，使用这个序列化过度，2个权限实际是一样的
    private Collection<String> customAuthorities;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setSystemAuthorities() {
        for (String customAuthority : customAuthorities) {
            this.authorities.add(new SimpleGrantedAuthority(customAuthority));
        }
    }

    public LoginUser(String username, String password) {
        this.password = password;
        this.username = username;
    }

    public LoginUser() {}

    public Collection<String> getCustomAuthorities() {
        return customAuthorities;
    }

    public void setCustomAuthorities(Collection<String> customAuthorities) {
        this.customAuthorities = customAuthorities;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "LoginUser{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\''
            + ", type='" + type + '\'' + ", authorities=" + authorities + '}';
    }
}
